[TOC]

Reading Array Elements

An array is a collection of elements organized in a rectangle grid that spans across multiple dimensions. You can read a value from an array or assign a value to an element. It is also possible to read values from, or to assign values to, a set of elements. For examples, replacing a row of a matrix with another row, and deleting a matrix from a 3D array.

Subscript Indexing

This indexing method is easy to understand, but it can be somewhat inefficient and inflexible. If you are working with large arrays, we recommend using linear indexing whenever it is feasible.

Single Element

Let us consider an illustrative example above. It shows the array A with ndims(A) equal to 3. You may consider A as stacking three matrices in the direction pointing into the screen of your computer/device. Each element in the array is addressed by a 3-tuple of numbers, namely, . These numbers are called subscripts. The first subscrpt refers to the first dimension and so on. If A had dimensions, i.e., ndims(A) == m, then an element should be addressed by an -tuple of subscripts.

subscript indexing

For example, the element in the 1st row and 1st column of the 1st matrix is addressed by A(1,1,1). The element in the 3rd row of the 4th column of the 3rd matrix is address by A(3,4,3). In the code segment below, we first create a 3D array called A, and then read the elements A(1,1,1) and A(3,4,3):

Input
matrix1 = [1 4 7 10 13; 16 19 22 25 28; 31 34 37 40 43];
matrix2 = [2 5 8 11 14; 17 20 23 26 29; 32 35 38 41 44];
matrix3 = [3 6 9 12 15; 18 21 24 27 30; 33 36 39 42 45];
A(:,:,1) = matrix1
A(:,:,2) = matrix2
A(:,:,3) = matrix3
% Accessing elements
A(1,1,1)
A(3,4,3)
Output
ans = 
 1
 
ans = 
 42

You can replace the values of A(1,1,1) and A(3,4,3) using the code below:

A(1,1,1) = 999
A(3,4,3) = 888

Multiple Elements

To access multiple elements, use a vector of subscripts. For example, A([1,2], 3, 2) gives elements in the 1st and 2nd rows of the 3rd column in the 2nd matrix.

When a subscript is not given explicitly, it is assumed 1. For examples,

When any of the given subscript is empty, an empty array is returned. For examples,

Linear Indexing

Linear indexing is more efficient than subscript indexing, since it does not require converting a subscript to the corresponding memory address at which numbers are stored. Such a conversion requires potentially time consuming operations.

The example below illustrates how linear indexing works. An element of the array A is addressed by a single integer, called a linear index. The index corresponds to (1,1,1), as seen from the top-left corner of A(:,:,1) below. When is incremented to 2, it moves to (2,1,1) in the next row. When it finishes all rows in a column, it moves to the next column. The arrows in the figure below show the elements corrsponding to the linear index as it is incremented. There are in total 45 elements in the array A. Hence, corresponds to the last element in (3,5,3).

linear indexing

Multiple Elements

It is possible to access multiple elements by using multiple indexes, such as A([1 3 4]). In general, in A(ind), if ind is a vector and A is column (row) vector, the output array is a column (row) vector. Otherwise, the output array should always have the same size as the index array.

Operators end and :

The discussion below refers to A given by the following figure.

subscript indexing